{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check Whether Two Strings are Almost Equivalent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: checkAlmostEquivalent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #检查两个字符串是否几乎相等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>如果两个字符串 <code>word1</code>&nbsp;和 <code>word2</code>&nbsp;中从 <code>'a'</code>&nbsp;到 <code>'z'</code>&nbsp;每一个字母出现频率之差都 <strong>不超过</strong>&nbsp;<code>3</code>&nbsp;，那么我们称这两个字符串&nbsp;<code>word1</code> 和&nbsp;<code>word2</code> <strong>几乎相等</strong>&nbsp;。</p>\n",
    "\n",
    "<p>给你两个长度都为&nbsp;<code>n</code>&nbsp;的字符串&nbsp;<code>word1</code> 和&nbsp;<code>word2</code>&nbsp;，如果&nbsp;<code>word1</code>&nbsp;和&nbsp;<code>word2</code>&nbsp;<strong>几乎相等</strong>&nbsp;，请你返回&nbsp;<code>true</code>&nbsp;，否则返回&nbsp;<code>false</code>&nbsp;。</p>\n",
    "\n",
    "<p>一个字母 <code>x</code>&nbsp;的出现 <strong>频率</strong>&nbsp;指的是它在字符串中出现的次数。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>word1 = \"aaaa\", word2 = \"bccb\"\n",
    "<b>输出：</b>false\n",
    "<b>解释：</b>字符串 \"aaaa\" 中有 4 个 'a' ，但是 \"bccb\" 中有 0 个 'a' 。\n",
    "两者之差为 4 ，大于上限 3 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>word1 = \"abcdeef\", word2 = \"abaaacc\"\n",
    "<b>输出：</b>true\n",
    "<b>解释：</b>word1 和 word2 中每个字母出现频率之差至多为 3 ：\n",
    "- 'a' 在 word1 中出现了 1 次，在 word2 中出现了 4 次，差为 3 。\n",
    "- 'b' 在 word1 中出现了 1 次，在 word2 中出现了 1 次，差为 0 。\n",
    "- 'c' 在 word1 中出现了 1 次，在 word2 中出现了 2 次，差为 1 。\n",
    "- 'd' 在 word1 中出现了 1 次，在 word2 中出现了 0 次，差为 1 。\n",
    "- 'e' 在 word1 中出现了 2 次，在 word2 中出现了 0 次，差为 2 。\n",
    "- 'f' 在 word1 中出现了 1 次，在 word2 中出现了 0 次，差为 1 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>word1 = \"cccddabba\", word2 = \"babababab\"\n",
    "<b>输出：</b>true\n",
    "<b>解释：</b>word1 和 word2 中每个字母出现频率之差至多为 3 ：\n",
    "- 'a' 在 word1 中出现了 2 次，在 word2 中出现了 4 次，差为 2 。\n",
    "- 'b' 在 word1 中出现了 2 次，在 word2 中出现了 5 次，差为 3 。\n",
    "- 'c' 在 word1 中出现了 3 次，在 word2 中出现了 0 次，差为 3 。\n",
    "- 'd' 在 word1 中出现了 2 次，在 word2 中出现了 0 次，差为 2 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == word1.length == word2.length</code></li>\n",
    "\t<li><code>1 &lt;= n &lt;= 100</code></li>\n",
    "\t<li><code>word1</code> 和&nbsp;<code>word2</code>&nbsp;都只包含小写英文字母。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-whether-two-strings-are-almost-equivalent](https://leetcode.cn/problems/check-whether-two-strings-are-almost-equivalent/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-whether-two-strings-are-almost-equivalent](https://leetcode.cn/problems/check-whether-two-strings-are-almost-equivalent/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"aaaa\"\\n\"bccb\"', '\"abcdeef\"\\n\"abaaacc\"', '\"cccddabba\"\\n\"babababab\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        freq = defaultdict(int)   # 频数差哈希表\n",
    "        for ch in word1:\n",
    "            freq[ch] += 1\n",
    "        for ch in word2:\n",
    "            freq[ch] -= 1\n",
    "        # 判断每个字符频数差是否均小于等于 3\n",
    "        return all(abs(x) <= 3 for x in freq.values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        str_one = set(word1+word2)\n",
    "        len_word1 = len(word1)\n",
    "        len_word2 = len(word2)\n",
    "        for i in str_one:\n",
    "            a = len_word1-len(word1.replace(i,''))\n",
    "            b = len_word2-len(word2.replace(i,''))\n",
    "            if abs(a-b)>3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        freq = defaultdict(int)\n",
    "        for c in word1:\n",
    "            freq[c] += 1\n",
    "        for c in word2:\n",
    "            freq[c] -= 1\n",
    "        return all(abs(x) <= 3 for x in freq.values()) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        freq = defaultdict(int)\n",
    "        for ch in word1:\n",
    "            freq[ch] +=1 \n",
    "        for ch in word2 :\n",
    "            freq[ch] -= 1 \n",
    "        return all(abs(x) <= 3 for x in freq.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        cnt=Counter(word1)\n",
    "        for x in word2:\n",
    "            cnt[x]-=1\n",
    "        return all(abs(x)<=3 for y,x in cnt.items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        freq1 = Counter(word1)\n",
    "        freq2 = Counter(word2)\n",
    "        freq1.subtract(freq2)\n",
    "        if max(freq1.values()) < 4 and min(freq1.values()) > -4:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        for i in range(97,123):\n",
    "            num1 = word1.count(chr(i))\n",
    "            num2 = word2.count(chr(i))\n",
    "            if abs(num1-num2) > 3:\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        cnt1=Counter(word1)\n",
    "        cnt2=Counter(word2)\n",
    "        s=set(word1)|set(word2)\n",
    "        for i in s:\n",
    "            if abs(cnt1[i]-cnt2[i])>3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        count1 = Counter(word1)\n",
    "        count2 = Counter(word2)\n",
    "        for k in count1.keys():\n",
    "            if abs(count1[k]-count2[k]) > 3:\n",
    "                return False\n",
    "        for k in count2.keys():\n",
    "            if abs(count1[k]-count2[k]) > 3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "\n",
    "        def func(w1, w2):\n",
    "            dic1 = {}\n",
    "            for c in w1:\n",
    "                if c not in dic1:\n",
    "                    dic1[c] = 1\n",
    "                else:\n",
    "                    dic1[c] = dic1[c] + 1\n",
    "            \n",
    "            for k, v in dic1.items():\n",
    "                if k in w2: \n",
    "                    if abs(w2.count(k) - v) > 3:\n",
    "                        return False\n",
    "                else:\n",
    "                    if v > 3:\n",
    "                        return False\n",
    "            return True\n",
    "        \n",
    "        if func(word1, word2) and func(word2, word1):\n",
    "            return True\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        word1_map, word2_map= Counter(word1), Counter(word2)\n",
    "        return  all(abs(word1_map[char] - word2_map[char]) <= 3  \n",
    "                        for char in word1_map.keys() | word2_map.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        c_word1 = [0]*26\n",
    "        c_word2 = [0]*26\n",
    "        for i in word1:\n",
    "            c_word1[ord(i)-97] += 1\n",
    "        for i in word2:\n",
    "            c_word2[ord(i)-97] += 1\n",
    "        for i in range(0,26):\n",
    "            if abs(int(c_word1[i]) - int(c_word2[i])) > 3:\n",
    "                return False\n",
    "        return True\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import string\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        c1=Counter(word1)\n",
    "        c2=Counter(word2)\n",
    "        for c in string.ascii_lowercase:\n",
    "            if abs(c1.get(c,0)-c2.get(c,0))>3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        dic1=Counter(word1)\n",
    "        dic2=Counter(word2)\n",
    "        for key,value in dic1.items():\n",
    "            if abs(word2.count(key)-value)>3:\n",
    "                return False\n",
    "        for key,value in dic2.items():\n",
    "            if abs(word1.count(key)-value)>3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        dic1 = {}\n",
    "        dic2 = {}\n",
    "        for i in range(ord(\"a\"),ord(\"z\")+1):\n",
    "            dic1[chr(i)] = 0\n",
    "            dic2[chr(i)] = 0\n",
    "        for i in word1:\n",
    "            dic1[i] += 1\n",
    "        for i in word2:\n",
    "            dic2[i] += 1\n",
    "        for i in dic1:\n",
    "            if abs(dic1[i]-dic2[i]) > 3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        cnt1 = Counter(word1)\n",
    "        cnt2 = Counter(word2)\n",
    "        for  i in range(26):\n",
    "            w = chr(i+ord(\"a\"))\n",
    "            if abs(cnt1[w]-cnt2[w])>3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        s = [0] * 26\n",
    "        for a, b in zip(word1, word2):\n",
    "            s[ord(a) - ord('a')] += 1\n",
    "            s[ord(b) - ord('a')] -= 1\n",
    "        return all(abs(x) <= 3 for x in s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        freq=defaultdict(int)\n",
    "        for ch in word1:\n",
    "            freq[ch]+=1\n",
    "        for ch in word2:\n",
    "            freq[ch]-=1\n",
    "        return all(abs(x)<=3 for x in freq.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        set1 = set(word1 + word2)\n",
    "        for i in set1:\n",
    "            if abs(word1.count(i) - word2.count(i)) > 3:\n",
    "                return False \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        a = 'qwertyuiopasdfghjklnbvcxmz'\n",
    "        for i in a:\n",
    "            if abs(word1.count(i)-word2.count(i)) >3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        cnt1,cnt2=Counter(word1),Counter(word2)\n",
    "        for k in cnt1:\n",
    "            if abs(cnt1[k]-cnt2[k])>3:\n",
    "                return False\n",
    "        for k in cnt2:\n",
    "            if cnt1[k]!=0:\n",
    "                continue\n",
    "            if cnt2[k]>3:\n",
    "                return False \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        #word1 转化为 由字典组成的列表 [{:},{:},{:},{:}]\n",
    "        list1 = list(word1)\n",
    "        dict1 ={}\n",
    "        for i in list1:\n",
    "            try:\n",
    "                dict1[i] +=1\n",
    "            except:\n",
    "                dict1[i] = 1\n",
    "        #word2 转化为 由字典组成的列表 [{:},{:},{:},{:}]        \n",
    "        list2 = list(word2)\n",
    "        dict2 ={}\n",
    "        for i in list2:\n",
    "            try:\n",
    "                dict2[i] +=1\n",
    "            except:\n",
    "                dict2[i] = 1\n",
    "\n",
    "\n",
    "        #dict1 和 dict2 补齐\n",
    "        set1 =(set(dict1.keys())|set(dict2.keys())) - set(dict1.keys())\n",
    "        for x in set1:\n",
    "            dict1[x] = 0\n",
    "        set2 =(set(dict1.keys())|set(dict2.keys())) - set(dict2.keys())\n",
    "        for x in set2:\n",
    "            dict2[x] = 0\n",
    "\n",
    "\n",
    "        #比较dict1 和 dict2 2个字典 对应key 的value 差，abs() 大于3 返回False\n",
    "\n",
    "        num_list =[]\n",
    "        for y in dict1:\n",
    "            num = abs(dict1[y] - dict2[y])\n",
    "            num_list.append(num)\n",
    "        if max(num_list) >3:\n",
    "            return False\n",
    "        else:\n",
    "            return True\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        word = word1+word2\n",
    "        dict1 = dict(collections.Counter(word1))\n",
    "        dict2 = dict(collections.Counter(word2))\n",
    "        for i in range(len(word)):\n",
    "            if abs(dict1.get(word[i],0)-dict2.get(word[i],0))>3:\n",
    "                return False\n",
    "        return True        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        set1 = set(word1 + word2)\n",
    "        for i in set1:\n",
    "            if abs(word1.count(i) - word2.count(i)) > 3:\n",
    "                return False \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        dic_1 = Counter(word1)\n",
    "        dic_2 = Counter(word2)\n",
    "        for i in (dic_1|dic_2):\n",
    "            if abs(dic_1[i]-dic_2[i]) > 3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        counter1,counter2 = Counter(word1),Counter(word2)\n",
    "        res1 = list((counter1 - counter2).values())\n",
    "        res2 = list((counter2 - counter1).values())\n",
    "        return max(res1,default=0) <= 3 and max(res2,default=0) <= 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        dic1 = Counter(word1)\n",
    "        dic2 = Counter(word2)\n",
    "        dic = dic1 - dic2 | dic2 - dic1\n",
    "        return all(v <= 3 for v in dic.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        \n",
    "        l_ = set(word1 + word2)\n",
    "\n",
    "        for i in l_:\n",
    "            if abs(word1.count(i) - word2.count(i)) > 3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        n1 = {}  # 存储word1中字母及出现的频率\n",
    "        n2 = {}  # 存储word2中字母及出现的频率\n",
    "        for w1 in word1:\n",
    "            n1[w1] = n1.get(w1, 0) + 1\n",
    "        for w2 in word2:\n",
    "            n2[w2] = n2.get(w2, 0) + 1\n",
    "        for char, freq1 in n1.items():\n",
    "            freq2 = n2.get(char, 0)\n",
    "            if abs(freq1 - freq2) > 3:\n",
    "                return False\n",
    "        for char, freq2 in n2.items():\n",
    "            freq1 = n1.get(char, 0)\n",
    "            if abs(freq2 - freq1) > 3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        ma=[0 for _ in range(26)]\n",
    "        for i in word1:\n",
    "            ma[ord(i)-ord('a')]+=1\n",
    "        \n",
    "        for i in word2:\n",
    "            ma[ord(i)-ord('a')]-=1\n",
    "        \n",
    "        for key in ma:\n",
    "            if abs(key)>3:\n",
    "                return False\n",
    "            \n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        ds1,ds2 = Counter(word1),Counter(word2)\n",
    "        ds = (ds1-ds2) + (ds2-ds1)\n",
    "        for _,v in ds.items():\n",
    "            if v > 3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        dic1 = Counter(word1)\n",
    "        dic2 = Counter(word2)\n",
    "        for k, v in dic1.items():\n",
    "            if abs(dic2[k] - v) > 3:\n",
    "                return False\n",
    "        for k, v in dic2.items():\n",
    "            if abs(dic1[k] - v) > 3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        my_str = word1 +word2\n",
    "        for i in my_str: \n",
    "            num1 = word1.count(i)\n",
    "            num2 = word2.count(i)\n",
    "            if abs(num1 - num2) > 3 :\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        freq = defaultdict(int)\n",
    "        for ch in word1:\n",
    "            freq[ch]+=1\n",
    "        for ch in word2:\n",
    "            freq[ch]-=1\n",
    "        return all(abs(x)<=3 for x in freq.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        counter1 = dict()\n",
    "        counter2 = dict()\n",
    "        for i in word1 :\n",
    "            if counter1.get(i) is None :\n",
    "                counter1[i] = 1 \n",
    "            else :\n",
    "                counter1[i] += 1\n",
    "        for i in word2 :\n",
    "            if counter2.get(i) is None :\n",
    "                counter2[i] = 1 \n",
    "            else :\n",
    "                counter2[i] += 1\n",
    "        for k , v in counter1.items() :\n",
    "            x = counter2.get(k) \n",
    "            if x is None and v > 3 :\n",
    "                return False\n",
    "            if x is None and v <= 3 :\n",
    "                continue\n",
    "            if abs(x-v) > 3 :\n",
    "                return False\n",
    "        for k , v in counter2.items() :\n",
    "            x = counter1.get(k) \n",
    "            if x is None and v > 3 :\n",
    "                return False\n",
    "            if x is None and v <= 3 :\n",
    "                continue\n",
    "            if abs(x-v) > 3 :\n",
    "                return False\n",
    "        return True \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        for x in word1:\n",
    "            #print(x)\n",
    "            if abs(word1.count(x)-word2.count(x))>3: return False\n",
    "        ylist=[y for y in word2 if y not in word1]\n",
    "        for y in ylist:\n",
    "            #print(y)\n",
    "            if abs(word1.count(y)-word2.count(y))>3: return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        def checkdict(d1, d2):\n",
    "            for c in d1:\n",
    "                if c in d2:\n",
    "                    if abs(d2[c] - d1[c]) > 3:\n",
    "                        return False\n",
    "                else:\n",
    "                    if d1[c] > 3:\n",
    "                        return False\n",
    "            return True\n",
    "        d1, d2 = Counter(word1), Counter(word2)\n",
    "        \n",
    "        return checkdict(d1, d2) and checkdict(d2, d1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        lst = [0] * 26\n",
    "        for c in word1:\n",
    "            lst[ord(c) - ord(\"a\")] += 1\n",
    "        for c in word2:\n",
    "            lst[ord(c) - ord(\"a\")] -= 1\n",
    "        return all(abs(n) <= 3 for n in lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        tmp = [0] * 26\n",
    "        for x in word1:\n",
    "            tmp[ord(x) - ord(\"a\")] += 1\n",
    "        for x in word2:\n",
    "            tmp[ord(x) - ord(\"a\")] -= 1\n",
    "        for i in tmp:\n",
    "            if abs(i) > 3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        b = ord('a')\n",
    "        r = [0] * 26\n",
    "        for c in word1:\n",
    "            v = ord(c) - b\n",
    "            r[v] += 1\n",
    "        for c in word2:\n",
    "            v = ord(c) - b\n",
    "            r[v] -= 1\n",
    "            if r[v] < -3:\n",
    "                return False\n",
    "        for v in r:\n",
    "            if v > 3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        d1 = collections.Counter(word1)\n",
    "        d2 = collections.Counter(word2)\n",
    "        for k, v in d1.items():\n",
    "            if abs(v - d2.get(k, 0)) > 3:\n",
    "                return False\n",
    "        for k, v in d2.items():\n",
    "            if abs(v - d1.get(k, 0)) > 3:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        set1 = set(word1)\n",
    "        set2 = set(word2)\n",
    "        set3 = set1 | set2\n",
    "\n",
    "        for i in set3:\n",
    "            if abs(word1.count(i) - word2.count(i)) >3:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        freq = defaultdict(int)\n",
    "        for ch in word1:\n",
    "            freq[ch] += 1\n",
    "        for ch in word2:\n",
    "            freq[ch] -= 1\n",
    "        return all(abs(x) <= 3 for x in freq.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        setWord = list(set(word1 + word2))\n",
    "        for i in range(len(setWord)):\n",
    "            if abs(word1.count(setWord[i])-word2.count(setWord[i])) > 3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        dict1 = dict()\n",
    "        dict2 = dict()\n",
    "        for ch in word1:\n",
    "            if ch not in dict1:\n",
    "                dict1[ch] = 1\n",
    "            else:\n",
    "                dict1[ch] += 1\n",
    "        for ch in word2:\n",
    "            if ch not in dict2:\n",
    "                dict2[ch] = 1\n",
    "            else:\n",
    "                dict2[ch] += 1\n",
    "        all_key = set(word1) | set(word2)\n",
    "        for ch in all_key:\n",
    "            if ch not in dict2:\n",
    "                if dict1[ch] > 3:\n",
    "                    return False\n",
    "            if ch not in dict1:\n",
    "                if dict2[ch] > 3:\n",
    "                    return False\n",
    "            if ch in dict1 and ch in dict2:\n",
    "                if abs(dict1[ch] - dict2[ch]) > 3:\n",
    "                    return False\n",
    "        return True\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        d1 = {}\n",
    "        d2 = {}\n",
    "        for c in word1:\n",
    "            if c in d1:\n",
    "                d1[c] += 1\n",
    "            else:\n",
    "                d1[c] = 1\n",
    "        \n",
    "        for c in word2:\n",
    "            if c in d2:\n",
    "                d2[c] += 1\n",
    "            else:\n",
    "                d2[c] = 1\n",
    "\n",
    "        \n",
    "        for k,v in d1.items():\n",
    "            n = d2.get(k)\n",
    "            if n is None:\n",
    "                if v > 3:\n",
    "                    return False\n",
    "                else:\n",
    "                    continue\n",
    "            if abs(v - n) > 3:\n",
    "                return False\n",
    "        \n",
    "        \n",
    "        for k,v in d2.items():\n",
    "            n = d1.get(k)\n",
    "            if n is None:\n",
    "                if v > 3:\n",
    "                    return False\n",
    "                else:\n",
    "                    continue\n",
    "                \n",
    "            if abs(v - n) > 3:\n",
    "                return False\n",
    "\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        counter1,counter2 = Counter(word1),Counter(word2)\n",
    "        res1 = list((counter1 - counter2).values())\n",
    "        res2 = list((counter2 - counter1).values())\n",
    "        return max(res1,default=0) <= 3 and max(res2,default=0) <= 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        # 将两个 word 转为 hash，再遍历进行比较\n",
    "        def turn2hash(s: str) -> dict:\n",
    "            d = {}\n",
    "            for i in s:\n",
    "                if i not in d:\n",
    "                    d[i] = 0;\n",
    "\n",
    "                d[i] += 1;\n",
    "            return d;\n",
    "\n",
    "        hash_word1, hash_word2 = turn2hash(word1), turn2hash(word2)\n",
    "\n",
    "        # 获取两者总共的 keys\n",
    "        total_chars = set([i for i in word1] + [j for j in word2])\n",
    "        for c in total_chars:\n",
    "            a = hash_word1.get(c, 0);\n",
    "            b = hash_word2.get(c, 0);\n",
    "            if abs(a -b) > 3:\n",
    "                return False\n",
    "\n",
    "        return True\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        counter1,counter2 = Counter(word1),Counter(word2)\n",
    "        res1 = list((counter1 - counter2).values())\n",
    "        res2 = list((counter2 - counter1).values())\n",
    "        return max(res1,default=0) <= 3 and max(res2,default=0) <= 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        dic_1 = {}\n",
    "        for i in word1:\n",
    "            dic_1[i] = dic_1.get(i, 0) + 1\n",
    "                \n",
    "        dic_2 = {}\n",
    "        for i in word2:\n",
    "            dic_2[i] = dic_2.get(i, 0) + 1\n",
    "\n",
    "\n",
    "        l = list(set(list(dic_1.keys()) + list(dic_2.keys())))\n",
    "        for i in l:\n",
    "            if abs(dic_1.get(i, 0) - dic_2.get(i, 0)) > 3:\n",
    "                return False\n",
    "        else:\n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        counter1,counter2 = Counter(word1),Counter(word2)\n",
    "        res1 = list((counter1 - counter2).values())\n",
    "        res2 = list((counter2 - counter1).values())\n",
    "        return max(res1,default=0) <= 3 and max(res2,default=0) <= 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        word1_dic = collections.Counter(word1)\n",
    "        word2_dic = collections.Counter(word2)\n",
    "        for char in word1_dic.keys() | word2_dic.keys():\n",
    "            print(char)\n",
    "            if abs(word1_dic[char] - word2_dic[char]) > 3:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        freq = defaultdict(int)   # 频数差哈希表\n",
    "        for ch in word1:\n",
    "            freq[ch] += 1\n",
    "        for ch in word2:\n",
    "            freq[ch] -= 1\n",
    "        # 判断每个字符频数差是否均小于等于 3\n",
    "        return all(abs(x) <= 3 for x in freq.values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        freq = defaultdict(int)   # 频数差哈希表\n",
    "        for ch in word1:\n",
    "            freq[ch] += 1\n",
    "        for ch in word2:\n",
    "            freq[ch] -= 1\n",
    "        # 判断每个字符频数差是否均小于等于 3\n",
    "        return all(abs(x) <= 3 for x in freq.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        ad1,ad2={},{}\n",
    "        for i in word1: ad1[i]=ad1.get(i,0)+1\n",
    "        for i in word2: ad2[i]=ad2.get(i,0)+1\n",
    "        \n",
    "        flag=True\n",
    "        for i in ad1.keys():\n",
    "            if abs(int(ad1.get(i,0))-int(ad2.get(i,0)))>3:\n",
    "                flag=False\n",
    "        for i in ad2.keys():\n",
    "            if abs(int(ad1.get(i,0))-int(ad2.get(i,0)))>3:\n",
    "                flag=False\n",
    "        return flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        \n",
    "        # l_ = set(word1 + word2)\n",
    "\n",
    "        # for i in l_:\n",
    "        #     if abs(word1.count(i) - word2.count(i)) > 3:\n",
    "        #         return False\n",
    "        # return True\n",
    "        freq = defaultdict(int)   # 频数差哈希表\n",
    "        for ch in word1:\n",
    "            freq[ch] += 1\n",
    "        for ch in word2:\n",
    "            freq[ch] -= 1\n",
    "        # 判断每个字符频数差是否均小于等于 3\n",
    "        return all(abs(x) <= 3 for x in freq.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        cnt1=Counter(word1)\n",
    "        cnt2=Counter(word2)\n",
    "        for i in range(26):\n",
    "            c=chr(ord('a')+i)\n",
    "            v=cnt1[c]-cnt2[c]\n",
    "        \n",
    "            if v>3 or v<-3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        wc = collections.Counter(word1)\n",
    "        # print(wc)\n",
    "        for i in word2:\n",
    "            wc[i] -= 1\n",
    "        # print(wc)\n",
    "        for _, val in wc.items():\n",
    "            if abs(val) > 3:\n",
    "                return False \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        mydic1=collections.Counter(word1)\n",
    "        mydic2=collections.Counter(word2)\n",
    "        for k,v in mydic1.items():\n",
    "            if k in mydic2.keys():\n",
    "                if (math.fabs(v-mydic2[k]))>3:\n",
    "                    return False\n",
    "            else:\n",
    "                if v>3:\n",
    "                    return False\n",
    "        \n",
    "        for k,v in mydic2.items():\n",
    "            if k in mydic1.keys():\n",
    "                if (math.fabs(v-mydic1[k]))>3:\n",
    "                    return False\n",
    "            else:\n",
    "                if v>3:\n",
    "                    return False\n",
    "        return True\n",
    "        \n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        s1 = set(word1)\n",
    "        s2 = set(word2)\n",
    "        s1.update(s2)\n",
    "\n",
    "        for i in s1:\n",
    "            if abs(word2.count(i)-word1.count(i)) > 3:\n",
    "                return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        freq = defaultdict(int)   # 频数差哈希表\n",
    "        for ch in word1:\n",
    "            freq[ch] += 1\n",
    "        for ch in word2:\n",
    "            freq[ch] -= 1\n",
    "        # 判断每个字符频数差是否均小于等于 3\n",
    "        return all(abs(x) <= 3 for x in freq.values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        c = Counter(word1)\n",
    "        c.subtract(word2)\n",
    "        for v in c.values():\n",
    "            if abs(v) > 3:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        cur = [0 for _ in range(26)]\n",
    "        n = len(word1)\n",
    "        for i in range(n):\n",
    "            i1 = ord(word1[i]) - 97\n",
    "            i2 = ord(word2[i]) - 97\n",
    "            cur[i1] += 1\n",
    "            cur[i2] -= 1\n",
    "        for j in cur:\n",
    "            if j < -3 or j > 3:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:\n",
    "        res=Counter(word1)\n",
    "        res.subtract(Counter(word2))\n",
    "        print(res)\n",
    "        for i,v in res.items():\n",
    "            if abs(v)>3:\n",
    "                return False\n",
    "        return True"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
